package com.chaoxuan.uebt.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.chaoxuan.uebt.R;
import com.chaoxuan.uebt.model.Address;
import com.chaoxuan.uebt.model.Store;
import com.chaoxuan.uebt.model.StoreOrder;
import com.chaoxuan.uebt.model.User;
import com.chaoxuan.uebt.model.Wallet;
import com.chaoxuan.uebt.utils.BaseSubscriber;
import com.chaoxuan.uebt.utils.Const;
import com.chaoxuan.uebt.utils.Loading;
import com.chaoxuan.uebt.utils.RetrofitUtils;
import com.chaoxuan.uebt.utils.CacheUtils;

public class Store1Activity extends BaseActivity implements View.OnClickListener{
    Store store;
    ImageView imageView;
    TextView tvStoreTitle,tvStorePay,tvStoreBalance,tvContactName,tvContactAddress;
    TextView tvOrderMoney,tvBalance,tvOrderPay;
    TextView editNumber;
    View btnMinus,btnPlus;
    Loading loading;
    Wallet wallet;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store1);
        initTitle();
        store=(Store)getIntent().getSerializableExtra("store");
        imageView=(ImageView)findViewById(R.id.img_store);
        tvStoreTitle=(TextView) findViewById(R.id.tv_store_title);
        tvStorePay=(TextView) findViewById(R.id.tv_store_pay);
        tvStoreBalance=(TextView) findViewById(R.id.tv_store_balance);

        tvOrderMoney=(TextView)findViewById(R.id.tv_order_money);
        tvBalance=(TextView)findViewById(R.id.tv_balance);
        tvOrderPay=(TextView)findViewById(R.id.tv_order_pay);

        tvContactName=(TextView) findViewById(R.id.tv_contact_name);
        tvContactAddress=(TextView) findViewById(R.id.tv_contact_address);
        btnMinus=findViewById(R.id.btn_minus);
        btnPlus=findViewById(R.id.btn_plus);
        btnMinus.setOnClickListener(this);
        btnPlus.setOnClickListener(this);
        //btnMinus.setSelected(true);
        editNumber=(TextView) findViewById(R.id.edit_number);
        findViewById(R.id.btn_confirm).setOnClickListener(this);
        findViewById(R.id.btn_modify).setOnClickListener(this);

        if(store!=null){
            tvStoreTitle.setText(store.getName());
            tvStorePay.setText(store.getPay());
            tvStoreBalance.setText("库存:"+store.getBalance()+"/"+store.getStock());
            Glide.with(this).load(store.getImg()).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.mipmap.default_info).error(R.mipmap.default_info).into(imageView);
        }
        loading=new Loading(this,R.style.dialog);
        wallet();
    }
    private void jia() {
        Double allIncome = Double.parseDouble(wallet.getAll_income());
        double balance=store.getBalance();
        double price=Double.parseDouble(store.getPay());
        int num = Integer.parseInt(editNumber.getText().toString());
        int count = num + 1;
        if (count > balance || (count * price) > allIncome) {

        } else {
            editNumber.setText(String.valueOf(count));
        }
        show();
    }

    private void jian() {
        int num = Integer.parseInt(editNumber.getText().toString());
        int count = num - 1;
        if (count > 0) {
            editNumber.setText(String.valueOf(count));
        }
        show();
    }

    private void show() {
        if(wallet==null||store==null)return;
        Double allIncome = Double.parseDouble(wallet.getAll_income());
        double balance=store.getBalance();
        double price=Double.parseDouble(store.getPay());
        int num = Integer.parseInt(editNumber.getText().toString());
        int count = num + 1;
        if (count > balance || (count * price) > allIncome) {
            btnPlus.setSelected(true);
        } else {
            btnPlus.setSelected(false);
        }

        if ((num - 1) <= 0) {
            btnMinus.setSelected(true);
        } else {
            btnMinus.setSelected(false);
        }

        tvOrderMoney.setText(String.valueOf(num * price));
        tvOrderPay.setText(String.valueOf(num * price));
        double total=allIncome - num * price;
        tvBalance.setText(String.valueOf((total>0)?total:allIncome));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_minus:
                jian();
                break;
            case R.id.btn_plus:
                jia();
                break;
            case R.id.btn_confirm:
                payTrue();
                break;
            case R.id.btn_modify:
                startActivity(new Intent(this,AddressActivity.class));
                break;

        }
    }

    private void getUserAddress(){
        User user= CacheUtils.getInstance(this).getUser();
        if(user==null)return;
        subscribe(RetrofitUtils.getAPIService().getUserAddress(user.getToken(), Const.DEVICE_TYPE), new BaseSubscriber<Address>() {
            @Override
            public void onSuccess(Address data) {
                tvContactName.setText(data.getContact_name()+"   "+data.getContact_mobile());
                tvContactAddress.setText(data.getProvince()+data.getCity()+data.getAddress());
            }
        });
    }


    private void payTrue(){
        User user= CacheUtils.getInstance(this).getUser();
        if(user==null)return;
        loading.show();
        subscribe(RetrofitUtils.getAPIService().payTrue(user.getToken(), store.getId(), 1, Const.DEVICE_TYPE), new BaseSubscriber<StoreOrder>() {
            @Override
            public void onCompleted() {
                super.onCompleted();
                loading.dismiss();
            }
            @Override
            public void onSuccess(StoreOrder storeOrder) {
                Intent i= new Intent(getBaseContext(),StoreSuccessActivity.class);
                i.putExtra("title",store.getName());
                i.putExtra("type",1);
                i.putExtra("pay",store.getPay());
                i.putExtra("id",storeOrder.getOid());
                startActivity(i);
            }

        });
    }

    private void wallet(){
        User user= CacheUtils.getInstance(this).getUser();
        if(user==null)return;
        loading.show();
        subscribe(RetrofitUtils.getAPIService().wallet(user.getToken(),Const.DEVICE_TYPE), new BaseSubscriber<Wallet>() {
            @Override
            public void onCompleted() {
                super.onCompleted();
                loading.hide();
            }

            @Override
            public void onSuccess(Wallet data) {
                wallet=data;
                show();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getUserAddress();
    }
}
